<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <button id="btn">按钮</button>
    <!-- 定时器中解决this指向问题
        1.bind() 
        2.箭头函数
     -->
    <script>
        var id="1001"
        var btn = document.getElementById("btn")
        btn.onclick = function(){
            setTimeout(function(){   
                console.log(this.id);   
            }.bind(btn),1000)
        }

        /* btn.onclick = function(){
            var self = this
            setTimeout(function(){   
                console.log(this.id);   
            }.bind(self),1000)
        } 
        
        btn.onclick = function(){
            setTimeout(function(){   
                console.log(this.id);   
            }.bind(this),1000)
        } 
        */
       /* 箭头函数  
       解决了this指向的问题 */
       btn.onclick = function(){
            setTimeout(()=>{
                console.log(this.id);
            },1000)
        }
    </script>
</body>
</html>